const global = getApp().globalData;
const wxApi = global.wxApi;
const Api = global.Api;
const util = global.util;

const attentionModes = require('../../config/attentionModes');

Page({
  data: {
    levels: []
  },
  onLoad( options ) {
    let mode = attentionModes[ options.mode ];
    this.mode = mode;
    console.log( mode );
    wxApi.setNavTitle( mode.name );
    
  },
  onShow() {
    this.init();
  },
  init() {
    this.loadMyGrades( this.mode.id );
  },
  onSelectMode( e ) {
    let dataset = e.currentTarget.dataset;
    let level = dataset.level;
    let path = '../attention-play/attention-play';
    let params = {
      level: level,
      mode: this.mode
    }
    params = util.encodeURI( params );
    wxApi.navigateTo( path, {
      params: params
    })
  },
  onTapRanks() {
    let path = '../attention-ranks/attention-ranks';
    wxApi.navigateTo( path, {
      modeIndex: this.mode.index
    } );
  },
  loadMyGrades( mode ) {
    console.log( mode );
    Api.getMyGrades( mode, levels => {
      console.log( levels );
      this.setLevels( levels );
    } )
  },
  setLevels( levels ) {
    let arr = [];
    for ( let i = 0, len = levels.length; i < len; i++ ) {
      let lev = levels[ i ];
      let myBest = lev.myBest;
      if ( myBest != 0 ) {
        let time = util.msecFormat( myBest );
        myBest = time.minute + ':' + time.second + '.' + time.ms;
        lev.myBestText = myBest;
      } else {
        lev.myBestText = '';
      }
      
      arr.push( lev );
    }
    this.setData({
      levels: arr
    })
  }
})